package com.bhprod.comatule.search;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bhprod.comatule.R;
import com.bhprod.comatule.model.Taxon;

/**
 * Generate the taxons list content
 * @author MonkeyKiki
 *
 */
public class TaxonsListAdapter extends BaseExpandableListAdapter {
	
	private Context context;
	private List<Taxon> taxons = new ArrayList<Taxon>();
	private List<Integer> selectedGroupPositionInCurrentLevel = new ArrayList<Integer>();
	
	public List<Integer> getSelectedGroupPositionInCurrentLevel() {
		return selectedGroupPositionInCurrentLevel;
	}

	public void setSelectedGroupPositionInCurrentLevel(List<Integer> selectedGroupPositionInCurrentLevel) {
		this.selectedGroupPositionInCurrentLevel = selectedGroupPositionInCurrentLevel;
	}

	public TaxonsListAdapter(final Context context) {
		this.context = context;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		 return taxons.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		 return 0;
	}

	/**
	 * Display taxon detail
	 * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		Taxon taxon = (Taxon) getChild(groupPosition, childPosition);
		
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.search_group_child_item, null);
        }
        
        TextView tv = (TextView) convertView.findViewById(R.id.itemDetailDescription);
        tv.setText(taxon.getShortDescription());
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        
        tv = (TextView) convertView.findViewById(R.id.itemDetailTaxonSpecies);
        tv.setText(taxon.getChildrenText());
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        
        tv = (TextView) convertView.findViewById(R.id.itemDetailCriteria);
        tv.setText(taxon.getCriteria());
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

        return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return taxons.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return taxons.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	/** Display taxon
	 * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		Taxon taxon = (Taxon) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.search_group_item, null);
        }
        TextView tvName = (TextView) convertView.findViewById(R.id.groupName);
        tvName.setText(taxon.getName());
        tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        
        TextView tvChildren = (TextView) convertView.findViewById(R.id.groupChildren);
       	tvChildren.setText("(sp. " + taxon.getNbSubSpecies() + ")");
        tvChildren.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        
        // Allow group item AND group checkbox to be clickable
        CheckBox cb = (CheckBox)convertView.findViewById(R.id.groupCheckBox);
    	cb.setFocusable(false);
    	cb.setTag(groupPosition);
        if (taxon.getNbSubSpecies() > 0) {
        	cb.setVisibility(View.VISIBLE);
        	
        	if (selectedGroupPositionInCurrentLevel.contains(groupPosition)) {
        		cb.setChecked(true);
        	} else {
        		cb.setChecked(false);
        	}
        } else {
        	cb.setVisibility(View.INVISIBLE);
        }
        
        return convertView;
	}
	
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		
		selectedGroupPositionInCurrentLevel.clear();
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void setTaxons(List<Taxon> taxons) {
		this.taxons = taxons;
	}

	public List<Taxon> getTaxons() {
		return taxons;
	}

}
